﻿EntityPlayer = Eg.Entity.extend({
  name: 'player',
  size: {
    x: 8,
    y: 14
  },
  offset: {
    x: 4,
    y: 2
  },
  maxVel: {
    x: 60,
    y: 240
  },
  accelDef: {
    ground: 400,
    air: 200
  },
  frictionDef: {
    ground: 400,
    air: 100
  },
  jump: 120,
  bounciness: 0,
  health: 10,
  type: Eg.Entity.TYPE.A,
  checkAgainst: Eg.Entity.TYPE.NONE,
  collides: Eg.Entity.COLLIDES.PASSIVE,
  flip: false,
  flippedAnimOffset: 24,
  idle: false,
  moved: false,
  wasStanding: false,
  canHighJump: false,
  highJumpTimer: null,
  idleTimer: null,
  sfxPlasma: new Eg.Sound('Resource/plasma.ogg'),
  sfxDie: new Eg.Sound('Resource/die-respawn.ogg', false),
  animSheet: new Eg.AnimationSheet('Resource/player.png', 16, 16),
  init: function (x, y, settings) {
    this.friction.y = 0;
    this.parent(x, y, settings);
    this.idleTimer = new Eg.Timer();
    this.highJumpTimer = new Eg.Timer();
    this.addAnim('idle', 1, [0]);
    this.addAnim('scratch', 0.3, [2, 1, 2, 1, 2], true);
    this.addAnim('shrug', 0.3, [3, 3, 3, 3, 3, 3, 4, 3, 3], true);
    this.addAnim('run', 0.07, [6, 7, 8, 9, 10, 11]);
    this.addAnim('jump', 1, [15]);
    this.addAnim('fall', 0.4, [12, 13]);
    this.addAnim('land', 0.15, [14]);
    this.addAnim('die', 0.07, [18, 19, 20, 21, 22, 23, 16, 16, 16]);
    this.addAnim('spawn', 0.07, [16, 16, 16, 23, 22, 21, 20, 19, 18]);
  },
  update: function () {
    if (this.currentAnim == this.anims.spawn) {
      this.currentAnim.update();
      if (this.currentAnim.loopCount) {
        this.currentAnim = this.anims.idle.rewind();
      } else {
        return;
      }
    }
    if (this.currentAnim == this.anims.die) {
      this.currentAnim.update();
      if (this.currentAnim.loopCount) {
        this.kill();
      }
      return;
    }
    this.moved = false;
    this.friction.x = this.standing ? this.frictionDef.ground : this.frictionDef.air;
    if (Eg.input.state('left')) {
      this.accel.x = -(this.standing ? this.accelDef.ground : this.accelDef.air);
      this.flip = true;
      this.moved = true;
    } else if (Eg.input.state('right')) {
      this.accel.x = (this.standing ? this.accelDef.ground : this.accelDef.air);
      this.flip = false;
      this.moved = true;
    } else {
      this.accel.x = 0;
    }
    if (Eg.input.pressed('shoot')) {
      var x = this.pos.x + (this.flip ? -3 : 5);
      var y = this.pos.y + 6;
      Eg.game.spawnEntity('projectile', EntityProjectile, x, y, {
        flip: this.flip
      });
      this.sfxPlasma.play();
    }
    this.wantsJump = this.wantsJump || Eg.input.pressed('jump');
    if (this.standing && (Eg.input.pressed('jump') || (!this.wasStanding && this.wantsJump && Eg.input.state('jump')))) {
      //Eg.mark('jump');
      this.wantsJump = false;
      this.canHighJump = true;
      this.highJumpTimer.set(0.14);
      this.vel.y = -this.jump / 4;
    } else if (this.canHighJump) {
      var d = this.highJumpTimer.delta();
      if (Eg.input.state('jump')) {
        var f = Math.max(0, d > 0 ? Eg.sys.tick - d : Eg.sys.tick);
        this.vel.y -= this.jump * f * 6.5;
      } else {
        this.canHighJump = false;
      }
      if (d > 0) {
        this.canHighJump = false;
      }
    }
    this.wasStanding = this.standing;
    this.parent();
    
    this.setAnimation();
  },
  setAnimation: function () {
    if ((!this.wasStanding && this.standing)) {
      this.currentAnim = this.anims.land.rewind();
    } else if (this.standing && (this.currentAnim != this.anims.land || this.currentAnim.loopCount > 0)) {
      if (this.moved) {
        if (this.standing) {
          this.currentAnim = this.anims.run;
        }
        this.idle = false;
      } else {
        if (!this.idle || this.currentAnim.stop && this.currentAnim.loopCount > 0) {
          this.idle = true;
          this.idleTimer.set(Math.random() * 4 + 3);
          this.currentAnim = this.anims.idle;
        }
        if (this.idleTimer.delta() > 0) {
          this.idleTimer.reset();
          this.currentAnim = [this.anims.scratch, this.anims.shrug].random().rewind();
        }
      }
    } else if (!this.standing) {
      if (this.vel.y < 0) {
        this.currentAnim = this.anims.jump;
      } else {
        if (this.currentAnim != this.anims.fall) {
          this.anims.fall.rewind();
        }
        this.currentAnim = this.anims.fall;
      }
      this.idle = false;
    }
    this.currentAnim.flip.x = this.flip;
    if (this.flip) {
      this.currentAnim.tile += this.flippedAnimOffset;
    }
  },
  collideWith: function (other, axis) {
    if (axis == 'y' && this.standing && this.currentAnim != this.anims.die) {
      this.currentAnim.update();
      this.setAnimation();
    }
  },
  receiveDamage: function (amount, from) {
    if (this.currentAnim != this.anims.die) {
      this.currentAnim = this.anims.die.rewind();
      for (var i = 0; i < 3; i++) {
        Eg.game.spawnEntity('gib', EntityPlayerGib, this.pos.x, this.pos.y);
      }
      Eg.game.spawnEntity('gib', EntityPlayerGibGun, this.pos.x, this.pos.y);
      //this.sfxDie.play();
    }
  },
  kill: function () {
    this.parent();
    Eg.game.respawnPlayerAtLastCheckpoint(this.pos.x, this.pos.y);
  }
});